package cn.schoolwow.quickflow.service.executor.putData;

import cn.schoolwow.quickflow.QuickFlowExecutor;
import cn.schoolwow.quickflow.domain.FlowContextDataOption;
import cn.schoolwow.quickflow.domain.FlowDataFeature;
import cn.schoolwow.quickflow.domain.FlowExecutorConfig;
import cn.schoolwow.quickflow.dto.FlowContextDataRange;
import cn.schoolwow.quickflow.util.QuickFlowUtil;

import java.util.Map;

public class QuickFlowExecutorPutDataOperationImpl implements QuickFlowExecutorPutDataOperation{
    private FlowExecutorConfig flowExecutorConfig;

    public QuickFlowExecutorPutDataOperationImpl(FlowExecutorConfig flowExecutorConfig) {
        this.flowExecutorConfig = flowExecutorConfig;
    }

    @Override
    public QuickFlowExecutor putThreadLocalData(String key, Object value, FlowDataFeature... flowDataFeatures) {
        flowExecutorConfig.flowContext.putThreadLocalData(key, value, flowDataFeatures);
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor putCurrentFlowData(String key, Object value, FlowDataFeature... flowDataFeatures) {
        flowExecutorConfig.flowContext.putCurrentFlowData(key, value, flowDataFeatures);
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor putCurrentCompositeFlowData(String key, Object value, FlowDataFeature... flowDataFeatures) {
        if(null==flowExecutorConfig.parentFlowExecutorConfig){
            flowDataFeatures = QuickFlowUtil.addFlowDataFeature(FlowDataFeature.RequestData, flowDataFeatures);
            flowExecutorConfig.flowContext.putData(key, value, flowDataFeatures);
        }else{
            flowExecutorConfig.flowContext.putCurrentCompositeFlowData(key, value, flowDataFeatures);
        }
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor putTemporaryData(String key, Object value) {
        flowExecutorConfig.flowContext.putTemporaryData(key, value);
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor putReturnData(String key, Object value) {
        flowExecutorConfig.flowContext.putReturnData(key, value);
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor putData(String key, Object value, FlowDataFeature... flowDataFeatures) {
        if(null==flowExecutorConfig.parentFlowExecutorConfig){
            flowDataFeatures = QuickFlowUtil.addFlowDataFeature(FlowDataFeature.RequestData, flowDataFeatures);
        }
        flowExecutorConfig.flowContext.putData(key, value, flowDataFeatures);
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor putContextThreadLocalData(String key, Object value, FlowDataFeature... flowDataFeatures) {
        flowExecutorConfig.flowContext.putContextThreadLocalData(key, value, flowDataFeatures);
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor putContextData(String key, Object value, FlowDataFeature... flowDataFeatures) {
        flowExecutorConfig.flowContext.putContextData(key, value, flowDataFeatures);
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor putContextTemporaryData(String key, Object value) {
        flowExecutorConfig.flowContext.putContextTemporaryData(key, value);
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor putFlowContextData(String key, Object value, FlowContextDataRange flowContextDataRange, FlowDataFeature... flowDataFeatures) {
        flowExecutorConfig.flowContext.putFlowContextData(key, value, flowContextDataRange, flowDataFeatures);
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor putFlowContextDataMap(Map<String, Object> dataMap, FlowContextDataRange flowContextDataRange, FlowDataFeature... flowDataFeatures) {
        flowExecutorConfig.flowContext.putFlowContextDataMap(dataMap, flowContextDataRange, flowDataFeatures);
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor putFlowContextData(FlowContextDataOption flowContextDataOption) {
        flowExecutorConfig.flowContext.putFlowContextData(flowContextDataOption);
        return flowExecutorConfig.quickFlowExecutor;
    }

}
